#alexander f. gazmararian
#afg2@princeton.edu
#January 9, 2024

#Load packages
library(tidyverse)
library(tidylog)
library(here)
library(gridExtra)

#load data
tv <- read.csv(here("data", "input", "tv", "tvnews_20230323175236.csv"))
#create variable for channel
tv <- separate(data=tv, col=Query, into=c("text", "channel"), sep = "AND")
#clean up variables
tv$text <- trimws(gsub("text=|\"", "", tv$text))
tv$channel <- trimws(gsub("channel=|\"", "", tv$channel))
#create variables
tv$Time <- as.Date(tv$Time)
#create plot
p.tv <-
  tv %>%
  mutate(
    text = case_when(
      text == "text = coal & (regulation | EPA)" ~ "coal AND regulation OR EPA",
      T ~ "coal AND fracking"
    ),
    text = factor(
      text,
      ordered = TRUE,
      levels = 
        c(
          "coal AND regulation OR EPA", "coal AND fracking"
        
      )
    )
  ) %>%
  ggplot(aes(x=Time,y=Value,color=channel,lty=channel)) +
  geom_line(size=1) +
  facet_wrap(~factor(text, levels = c("coal AND regulation OR EPA", "coal AND fracking"))) +
  theme_bw(base_size = 14) +
  scale_color_grey() +
  labs(x="Time",y="Seconds of Cable TV",color="",title="Coverage over Time",lty="") +
  scale_y_continuous(expand = c(0,0), limits = c(0,700)) +
    geom_label(
      data = data.frame(
        Time = as.Date("2015-01-01"),
        Value = 300,
        text = "coal AND fracking",
        channel = "FOX"
      ),
      label = "Clinton Fossil\nFuel Opposition",
      show.legend = FALSE,
      color = "black"
    ) +
    geom_label(
      data = data.frame(
        Time = as.Date("2020-01-01"),
        Value = 400,
        text = "coal AND fracking",
        channel = "FOX"
      ),
      label = "Biden Fracking\nOpposition",
      show.legend = FALSE,
      color = "black"
    ) +
    geom_label(
      data = data.frame(
        Time = as.Date("2011-01-01"),
        Value = 550,
        text = "coal AND regulation OR EPA",
        channel = "FOX"
      ),
      label = "EPA Regs\non Coal",
      show.legend = FALSE,
      color = "black"
    ) +
    geom_label(
      data = data.frame(
        Time = as.Date("2018-01-01"),
        Value = 400,
        text = "coal AND regulation OR EPA",
        channel = "FOX"
      ),
      label = "Trump Loosens\nCoal Regs",
      show.legend = FALSE,
      color = "black"
    ) +
    theme(
      panel.grid = element_blank(),
      legend.position = c(.95,.9),
      legend.background = element_rect(fill='transparent'),
      legend.box.background = element_rect(fill='transparent',color="transparent"),
      plot.title = element_text(hjust=.5)
      
    )
p.tv    
#text = "coal AND fracking"
#aggregate to years
p.pres <-
  tv %>%
  mutate(year = lubridate::year(Time)) %>%
  mutate(
    pres = case_when(
      year %in% c(2010,2011,2012,2013,2014,2015,2016)~"Obama",
      year %in% c(2017,2018,2019,2020) ~ "Trump",
      year %in% c(2021,2022,2023) ~ "Biden"
    )
  ) %>%
    mutate(
      text = case_when(
        text == "text = coal & (regulation | EPA)" ~ "coal AND regulation OR EPA",
        T ~ "coal AND fracking"
      ),
      text = factor(
        text,
        ordered = TRUE,
        levels = c(
          "coal AND regulation OR EPA", "coal AND fracking"
        )
      )
    ) %>%
  group_by(pres,channel,text) %>%
  summarize(sec = sum(Value)) %>%
  ggplot() +
  geom_col(aes(x=pres,y=sec/60,fill=channel),position="dodge") +
  facet_wrap(~text) +
  labs(x="Time",y="Minutes of Cable TV",fill="",title="Coverage by President") +
  scale_y_continuous(expand = c(0,0), limits = c(0, 30)) +
  theme_bw(base_size = 14) +
  theme(
    plot.title = element_text(hjust=.5),
    panel.grid = element_blank(),
    legend.position = c(.95,.9),
    legend.background = element_rect(fill='transparent'),
    legend.box.background = element_rect(fill='transparent',color="transparent"),
  ) +
  scale_fill_grey()
p.pres
p.out <- grid.arrange(p.tv,p.pres)
#Figure N2
ggsave(
  p.out,
  filename = here("output", "figures", "si_fig_N2_tv.png"),
  dpi = 300,
  scale = 1.5,
  width = 6.5,
  height = 5
) 
#Calculations in the Online Appendix
tv %>%
  mutate(year = lubridate::year(Time)) %>%
  mutate(
    pres = case_when(
      year %in% c(2010,2011,2012,2013,2014,2015,2016)~"Obama",
      year %in% c(2017,2018,2019,2020) ~ "Trump",
      year %in% c(2021,2022,2023) ~ "Biden"
    )
  ) %>%
  group_by(channel,text) %>%
  summarize(sec = sum(Value)) %>%
  mutate(sec=sec/60)
